//
//  shareViewController.m
//  PhyloHunter
//
//  Created by Sean Bramhall on 2013-06-20.
//
//	Programmer(s): Sean Bramhall
//	Team Name: The Gigglers (Group 14)
//
//	Changes:
/*		June 30
			- Changed from a temporary View Controller to a Collection View Controller
			- Created the Cells in story board
				- Cells need their corresponding Class Files to support/fill them
 */
//	Bugs: N/A
//
//	Task:	Is the Collection View Controller for the 'Share Card' main card selection screen

#import "shareCardViewController.h"
#import "shareCardCollectionViewCell.h"
#import "shareCardCollectionHeaderView.h"
#import "showQRcodeViewController.h"
//#import "CGGeometry.h"

@interface shareCardViewController () {
	
	//Create arrays, for: Card Names, Card Descriptions, Card File Names, and whether the cards have been unlocked (powered-up)
    NSArray *card_LabelNames, *card_Descriptions, *card_FileNames, *cardQRcodeImage;
}

@end

@implementation shareCardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	
	//Array holding the File Names of each card image
	card_FileNames = [[NSArray alloc] initWithObjects:@"00.jpg", @"01.jpg", @"02.jpg", @"03.jpg", @"04.jpg", @"05.jpg", @"06.jpg", @"07.jpg", @"08.jpg", @"09.jpg", @"10.jpg", @"11.jpg", @"12.jpg", @"13.jpg", @"14.jpg", @"15.jpg", @"16.jpg", @"17.jpg", @"18.jpg", @"19.jpg", @"20.jpg", @"21.jpg", @"22.jpg", @"23.jpg", @"24.jpg", @"25.jpg", @"26.jpg", @"27.jpg", @"28.jpg", @"29.jpg", @"30.jpg", nil];
	
	//Array that holds the name of each card
	card_LabelNames = [[NSArray alloc] initWithObjects:@"Home Card", @"Black Bear", @"Blue Whale", @"Clark's Nutcracker", @"Climate Change", @"Dragonfly", @"Earthworm", @"European Honey Bee", @"Giant Kelp", @"Habitat Loss", @"Himalayan BlackBerry", @"Human", @"Moss", @"Killer Whale", @"Marbled Murrelet", @"Mosquito", @"Moss Mite", @"Mycorrhizal Fungi", @"Oil Spill", @"Pacific Krill", @"PhytoPlankton", @"Purple Sea Urchin", @"Salal", @"Sea Otter", @"Sockeye Salmon", @"Species Protection", @"Steller Sea Lion", @"Three-Spined Stickleback", @"WhiteBark Pine", @"Wildfire", @"Zooplankton", nil];
    
    //Array that holds the image of teh QR codes
    cardQRcodeImage=[[NSArray alloc] initWithObjects:@"bbmphylo00.png", @"bbmphylo01.png", @"bbmphylo02.png", @"bbmphylo03.png", @"bbmphylo04.png", @"bbmphylo05.png", @"bbmphylo06.png", @"bbmphylo07.png", @"bbmphylo08.png", @"bbmphylo09.png", @"bbmphylo10.png", @"bbmphylo11.png", @"bbmphylo12.png", @"bbmphylo13.png", @"bbmphylo14.png", @"bbmphylo15.png", @"bbmphylo16.png", @"bbmphylo17.png", @"bbmphylo18.png", @"bbmphylo19.png", @"bbmphylo20.png", @"bbmphylo21.png", @"bbmphylo22.png", @"bbmphylo23.png", @"bbmphylo24.png", @"bbmphylo25.png", @"bbmphylo26.png", @"bbmphylo27.png", @"bbmphylo28.png", @"bbmphylo29.png", @"bbmphylo30.png",  nil];
		
	
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//How many Cells does the share list need? Return size of Array that holds list of locked and unlocked cards
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   /* NSInteger m=0;
    NSInteger numberOfUnlockedcards=0;
    m=[card_FileNames count];
    NSInteger i=0;
   for(i=1; i<=m; i++)
    {
        if([[cardUnlocked objectAtIndex:i] isEqualToString:@"UnLocked"])
        numberOfUnlockedcards++;
    }
    return numberOfUnlockedcards;*/
    return [card_FileNames count];
}



//Build every cell: Card ThumbNail, Name

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
	//Each Card has its own Cell (ThumbNail, Name of Card, Short Description about the card)
    static NSString *cellIdentifier = @"shareCell";
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
       
    
    
        
        //Display the ThumbNail of the Card in the Cell
                 UIImageView *cardImageView = (UIImageView *)[cell viewWithTag:101];
                cardImageView.image = [UIImage imageNamed:[card_FileNames objectAtIndex:indexPath.item]];

        
        //Display the Name of the Card in the Cell
        UILabel *cardLabel = (UILabel *)[cell viewWithTag:6];
        cardLabel.text = [card_LabelNames objectAtIndex:indexPath.item];
    
    
            return cell;
    
	
}


//Creates the "Beaty Museum Deck" Header (can be used to create other deck headers, to split the viewer)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   UICollectionReusableView *reusableview = nil;
    

    if (kind == UICollectionElementKindSectionHeader) {
        shareCardCollectionHeaderView *shareCardViewerHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"shareCardViewerHeader" forIndexPath:indexPath];
        NSString *title = [[NSString alloc]initWithFormat:@"Beaty Museum Deck"];
        shareCardViewerHeader.card_HeaderTitle.text = title;
        UIImage *headerImage = [UIImage imageNamed:@"header_banner.png"];
      shareCardViewerHeader.Background_Image.image = headerImage;
        
        reusableview = shareCardViewerHeader;
  }
	
    return reusableview;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    //load the unlocked cards array
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"arrays.out"];
    NSMutableArray *cardUnlocked = [NSMutableArray arrayWithContentsOfFile:arrayPath];
    
    //if the card at the current index is unlocked
    if ([[cardUnlocked objectAtIndex:indexPath.row] isEqualToString:@"Unlocked"]) {
        //return the size of the view to have the card visible
        return CGSizeMake(298, 130);
        
    }
    //if the card is not unlocked, set the size of the view to 0 to hide the card
    return CGSizeMake(0,0);
    
    
    
}

//Send QR code information and Qr code image should be displayed
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	
	if ([segue.identifier isEqualToString:@"showQRcodeImage"]){
		NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
		NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
		showQRcodeViewController *destViewController = segue.destinationViewController;
        
		destViewController.card_QRcodeImage = [cardQRcodeImage objectAtIndex:indexPath.item];
        destViewController.card_QRcodeLabel=[card_LabelNames objectAtIndex:indexPath.row];
		[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
        
	}
}


//Share Card Viewer Close Screen Button
- (IBAction)closeShareCardViewer:(id)sender {
   [self dismissViewControllerAnimated:YES completion:NULL];
}@end
